gusucode.com > VC 实现可选择颜色的ComboBox > VC 实现可选择颜色的ComboBox/gusucode/ColorPickerCB.cpp

    //Download by http://www.NewXing.com

#include "stdafx.h"
#include "ColorPickerCB.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif



//  给颜色和名称结构数组赋值
SColorAndName	CColorPickerCB::ms_pColors[ CCB_MAX_COLORS ] =
{
	SColorAndName( RGB( 0x00, 0x00, 0x00 ),	"Black" ),
	SColorAndName( RGB( 0x80, 0x00, 0x00 ),	"Maroon" ),
	SColorAndName( RGB( 0x00, 0x80, 0x00 ),	"Green" ),
	SColorAndName( RGB( 0x80, 0x80, 0x00 ),	"Olive" ),
	SColorAndName( RGB( 0x00, 0x00, 0x80 ),	"Navy" ),
	SColorAndName( RGB( 0x80, 0x00, 0x80 ),	"Purple" ),
	SColorAndName( RGB( 0x00, 0x80, 0x80 ),	"Teal" ),
	SColorAndName( RGB( 0x80, 0x80, 0x80 ),	"Grey" ),
	SColorAndName( RGB( 0xC0, 0xC0, 0xC0 ),	"Silver" ),
	SColorAndName( RGB( 0xFF, 0x00, 0x00 ),	"Red" ),
	SColorAndName( RGB( 0x00, 0xFF, 0x00 ),	"Lime" ),
	SColorAndName( RGB( 0xFF, 0xFF, 0x00 ),	"Yellow" ),
	SColorAndName( RGB( 0x00, 0x00, 0xFF ),	"Blue" ),
	SColorAndName( RGB( 0xFF, 0x00, 0xFF ),	"Fushcia" ),
	SColorAndName( RGB( 0x00, 0xFF, 0xFF ),	"Aqua" ),
	SColorAndName( RGB( 0xFF, 0xFF, 0xFF ),	"White" ),
};


CColorPickerCB::CColorPickerCB()
{
	m_bInitialized = false;
}


CColorPickerCB::~CColorPickerCB()
{
}


BEGIN_MESSAGE_MAP(CColorPickerCB, CComboBox)
	//{{AFX_MSG_MAP(CColorPickerCB)
	ON_WM_CREATE()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CColorPickerCB message handlers

int CColorPickerCB::OnCreate( LPCREATESTRUCT pCStruct ) 
{
	if( CComboBox::OnCreate( pCStruct ) == -1 )				
		return( -1 );										

	return( 0 );											
}


void CColorPickerCB::PreSubclassWindow() 
{
	// Initialize Contents
	Initialize();											
	
	// Subclass Control
	CComboBox::PreSubclassWindow();							

	// Select First Item By Default
	SetCurSel( 0 );											

	return;
}

//初始化列表框,将颜色值和名称赋给它
void CColorPickerCB::Initialize( void )
{
	int		iAddedItem = -1;

	// If Already Initialized
	if( m_bInitialized )									
		return;												

	// For All Colors
	for( int iColor = 0; iColor < CCB_MAX_COLORS; iColor++ )
	{
		// Set Color Name/Text
		iAddedItem = AddString(	ms_pColors[iColor].m_cColor );					
		if( iAddedItem == CB_ERRSPACE )						
			break;											
		else												
			// Set Color RGB Value
			SetItemData( iAddedItem, ms_pColors[
					iColor ].m_crColor );					
	}
	// Set Initialized Flag
	m_bInitialized = true;									
}

//按照列表项的颜色值和名称重画列表框
void CColorPickerCB::DrawItem( LPDRAWITEMSTRUCT pDIStruct )
{
	static		CString	sColor;								

	CDC			dcContext;
	CRect		rItemRect( pDIStruct -> rcItem );
	CRect		rBlockRect( rItemRect );
	CRect		rTextRect( rBlockRect );
	CBrush		brFrameBrush;
	int			iFourthWidth = 0;
	int			iItem = pDIStruct -> itemID;
	int			iAction = pDIStruct -> itemAction;
	int			iState = pDIStruct -> itemState;
	COLORREF	crColor = 0;
	COLORREF	crNormal = GetSysColor( COLOR_WINDOW );
	COLORREF	crSelected = GetSysColor( COLOR_HIGHLIGHT );
	COLORREF	crText = GetSysColor( COLOR_WINDOWTEXT );

	// 联系 CDC对象
	if( !dcContext.Attach( pDIStruct -> hDC ) )				
		return;			

	//获得1/4的列表项宽度
	iFourthWidth = ( rBlockRect.Width() / 4 );				
	//创建黑色画刷
	brFrameBrush.CreateStockObject( BLACK_BRUSH );			

	//如果该条目被选择
	if( iState & ODS_SELECTED )								
	{										
		
		dcContext.SetTextColor(
				( 0x00FFFFFF & ~( crText ) ) );				
		dcContext.SetBkColor( crSelected );					
		dcContext.FillSolidRect( &rBlockRect, crSelected );	
	}
	else//如果没有被选择
	{
		dcContext.SetTextColor( crText );					
		dcContext.SetBkColor( crNormal );					
		dcContext.FillSolidRect( &rBlockRect, crNormal );	
	}
	//如果该项出于焦点状态
	if( iState & ODS_FOCUS )								
		dcContext.DrawFocusRect( &rItemRect );				

	//计算文字区域
	rTextRect.left += ( iFourthWidth + 2 );					
	rTextRect.top += 2;										
	
	//计算图像区域
	rBlockRect.DeflateRect( CSize( 2, 2 ) );				
	rBlockRect.right = iFourthWidth;						
	
	//显示文字(颜色名)
	if( iItem != -1 )										
	{
		GetLBText( iItem, sColor );							

		//如果处于不可用状态
		if( iState & ODS_DISABLED )							
		{
			crColor = GetSysColor( COLOR_INACTIVECAPTIONTEXT );
			dcContext.SetTextColor( crColor );				
		}
		else //如果处于一般状态
			crColor = GetItemData( iItem );					

		dcContext.SetBkMode( TRANSPARENT );				

		dcContext.TextOut( rTextRect.left, rTextRect.top,sColor );									

		//填充颜色区域
		dcContext.FillSolidRect( &rBlockRect, crColor );	
		
		//画边框
		dcContext.FrameRect( &rBlockRect, &brFrameBrush );	
	}
	//分离CDC对象
	dcContext.Detach();										
}


//获得选择项的颜色值
COLORREF CColorPickerCB::GetSelectedColorValue( void )
{
	//获得选择的条目索引
	int		iSelectedItem = GetCurSel();					

	//如果没有被选择
	if( iSelectedItem == CB_ERR )							
		return( RGB( 0, 0, 0 ) );	// Return Black

	return( GetItemData( iSelectedItem ) );	
}

//获得选择项的颜色名称
CString		CColorPickerCB::GetSelectedColorName( void )
{
	//获得选择的条目索引
	int		iSelectedItem = GetCurSel();					

	//如果没有被选择
	if( iSelectedItem == CB_ERR )							
		return( m_sColorName = afxEmptyString ); 

	GetLBText( iSelectedItem, m_sColorName );				

	return( m_sColorName );									
}

//设定指定颜色值对应的列表项的颜色值
void		CColorPickerCB::SetSelectedColorValue( COLORREF crClr )
{
	int		iItems = GetCount();
	
	for( int iItem = 0; iItem < iItems; iItem++ )
	{
		if( crClr == GetItemData( iItem ) )					
			SetCurSel( iItem );								
	}
	return;													
}


//设定指定颜色名称对应列表项的名称
void		CColorPickerCB::SetSelectedColorName( PCSTR cpColor )
{
	int		iItems = GetCount();
	CString	sColorName;

	for( int iItem = 0; iItem < iItems; iItem++ )
	{
		GetLBText( iItem, sColorName );						

		if( !sColorName.CompareNoCase( cpColor ) )			
			SetCurSel( iItem );							
	}
}

//删除指定颜色名称对应的列表项
bool CColorPickerCB::RemoveColor( PCSTR cpColor )
{
	int		iItems = GetCount();
	CString	sColor;
	bool	bRemoved = false;

	for( int iItem = 0; iItem < iItems; iItem++ )
	{
		GetLBText( iItem, sColor );							
		if( !sColor.CompareNoCase( cpColor ) )				
		{
			DeleteString( iItem );							
			bRemoved = true;							
			break;										
		}
	}
	return( bRemoved );										
}

//删除指定颜色值对应的列表项
bool		CColorPickerCB::RemoveColor( COLORREF crClr )
{
	int		iItems = GetCount();
	bool	bRemoved = false;

	for( int iItem = 0; iItem < iItems; iItem++ )
	{
		if( crClr == GetItemData( iItem ) )					
		{
			DeleteString( iItem );							
			bRemoved = true;								
			break;											
		}
	}
	return( bRemoved );										
}

//增加指定颜色名称和颜色值的列表项
int			CColorPickerCB::AddColor( PCSTR cpName, COLORREF crColor )
{
	int		iItem = -1;

	iItem = InsertString( -1, cpName );						
	if( iItem != LB_ERR )								
		SetItemData( iItem, crColor );						

	return( iItem );										
}